const request = require("../../../utils/request.js").request;
const router = require("../../../utils/router.js").router;
const timeUtil = require("../../../utils/timeUtil.js").timeUtil;

Page({

  data: {
    arrowImg: "../../../images/arrow.png",

    userModal: "",
    orders: [],
  },

  onShow: function() {
    const that = this;
    const userModal = wx.getStorageSync('user');
    request(router.getOrder, "GET", {
      all: true,
      userModal: userModal,
    }).then(function(res) {

      const code = res.data.code;

      if (code == 200) {
        const orders = res.data.data.orders;

        orders.forEach(function(order, index) {
          order.applyDate = timeUtil.DateStrToDate(order.applyDate);
          order.applyDate = timeUtil.toLocalDate(order.applyDate);
          order.applyDate = timeUtil.formatDateToStr(order.applyDate);
          order.useDate = timeUtil.DateStrToDate(order.useDate);
          order.useDate = timeUtil.toLocalDate(order.useDate);
          order.useDate = timeUtil.formatDateToStr(order.useDate);
          order.returnDate = timeUtil.DateStrToDate(order.returnDate);
          order.returnDate = timeUtil.toLocalDate(order.returnDate);
          order.returnDate = timeUtil.formatDateToStr(order.returnDate);
        })

        that.setData({
          orders: orders,
        })
      } else {
        wx.navigateTo({
          url: '../../login/login',
        })
      }
    })
  },

  handleSelectOrder: function(e) {
    var that = this;
    const orderIndex = e.currentTarget.dataset.index;
    const order = that.data.orders[orderIndex];
    var orderStr = JSON.stringify(order);
    var navigateUrl = "./myOrderDetail/myOrderDetail" + "?order=" + orderStr;
    wx.navigateTo({
      url: navigateUrl,
    })
  }
})